package com.benchmark.springboot.controller;

import com.power.common.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 阻塞任务测试
 *
 * @author yu
 */
@Slf4j
@RestController
public class BlockController {

    private static ExecutorService executorService = Executors.newFixedThreadPool(1);

    /**
     * 阻塞任务测试
     * @apiNote 可以使用arthas的thread | grep pool来查看等待线程
     * @return
     */
    @GetMapping("block")
    public CommonResult block(){
        executorService.submit(()->{
            while (true){

            }
        });
        Thread thread = new Thread(() -> {
            while (true) {
                log.debug("thread start");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 添加到线程
        executorService.submit(thread);
        return CommonResult.ok();
    }
}
